<?php
include('cfg/def.php');
include('cfg/cfg.php');

include_once('lib/utils.class.php');
include_once('lib/timer.class.php');
include_once('lib/xtemplate.class.php');

$timer = new Timer('page', TRUE, Timer::FORMAT_S);

include_once('lib/logger.class.php');
Logger::$log_file = $log_file_name;

include_once('models/user.class.php');

header('Content-Type: text/html; charset=utf-8');
header('Cache-Control: no-cache');

session_start();

// В запросах полученных через AJAX будет проверяться этот параметр
$_SESSION["AJAXABLE"] = "YES";

Logger::Write(__FILE__." ".__FUNCTION__." line: ".__LINE__, "POST: ".print_r($_POST, true));
Logger::Write(__FILE__." ".__FUNCTION__." line: ".__LINE__, "GET: ".print_r($_GET, true));

// убираем случайный мусор вначале и в конце
foreach($_POST as $key=>$val) $_POST[$key] = trim($val);

$u = new User();

$page  = Utils::get_param('page', 'msg');
$cmd  = Utils::get_param('cmd', 'x');

$xtpl = new XTemplate('xtpl/page.xtpl');
$xtpl->assign('root', ROOT_URL);
$xtpl->assign('page', $page);

// unset($_SESSION[UID]);

if($u->auth())
{
	// $xtpl->assign('title', $u->props["email"]);
	$xtpl->assign('uid', $_SESSION[UID]);
	$xtpl->assign('title', $u->props["company"]);
	$xtpl->assign('email', $u->props["email"]);
	if(strtoupper($cmd) == 'LOGOUT')
	{
		$u->logout();
		header('location: '.ROOT_URL);
	}
	else
	{
		if($u->is_super())
		{
			$xtpl->assign('puper', 'true');
			$xtpl->parse('root.sections.super');
		}
		else
		{
			$xtpl->assign('puper', 'false');
			$xtpl->parse('root.sections.user');
		}
		// $xtpl->assign('email', $u->props["email"]);
		$xtpl->parse('root.sections');
	}
	
	$xopt = new XTemplate('xtpl/'.$page.'_opt.xtpl');
	$xopt->assign('user_id', $_SESSION[UID]);
	$xopt->assign('company_id', $u->props["company_id"]);
	$xopt->parse('root');
	$xtpl->assign('option', $xopt->text('root'));
	
	$xtpl->parse('root.data');
}
else
{
	$xtpl->parse('root.login');
}


$xtpl->parse('root');
echo $xtpl->text('root');

